import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paly_android/base/base_widget.dart';
import 'package:paly_android/bean/project_tree_entity.dart';
import 'package:paly_android/bloc/project_bloc.dart';
import 'package:paly_android/route/routes.dart';

class ProjectPage extends BaseWidget {
  @override
  BaseWidgetState<BaseWidget> getState() => _ProjectPageState();
}

class _ProjectPageState extends BaseWidgetState<ProjectPage> {

  late ProjectBloc bloc;
  @override
  void initState() {
   bloc=ProjectBloc();
   bloc.getProjectTree();
  }

  List<Widget> getWarpProject(List<ProjectTreeEntity> data) {
    return List.generate(
      data.length,
      (index) => OutlinedButton(
          // style: ButtonStyle(
          //     foregroundColor: MaterialStateProperty.all(Colors.white),
          //     backgroundColor: MaterialStateProperty.all(Colors.blue),
          // ),

          onPressed: () {
            Navigator.of(context).pushNamed(MyRouter.PTL,arguments: {"title":data[index].name,"cid":data[index].id});
          },
          child: Text(data[index].name)),
    );
  }

  @override
  Widget baseBuild(BuildContext context) {
    return BlocProvider(create: (context)=>bloc,
    child: BlocBuilder<ProjectBloc, List<ProjectTreeEntity>>(
      builder: (context, state) {
        return RefreshIndicator(
            child: SingleChildScrollView(
                child: Padding(
                  padding: EdgeInsets.all(10),
                  child: Wrap(
                    spacing: 20,
                    children: getWarpProject(state),
                  ),
                )),
            onRefresh: () async {
              BlocProvider.of<ProjectBloc>(context).getProjectTree();
              //结束刷新
            });
      },
    ),);
  }

  @override
  String getAppBarTitle() => "项目";
}
